package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.itheima.POJO.Member;
import com.itheima.POJO.MemberInfo;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.PhysicalUploadService;
import com.itheima.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;


import java.io.IOException;
import java.util.*;

/**
 * @date:
 * @author: majiao
 * @description:体检上传
 */
@RestController
@RequestMapping("/physicalUpload")
public class PhysicalUploadController {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private PhysicalUploadService physicalUploadService;

    @RequestMapping("/findMemberById")
    //根据会员id查询会员信息回显数据
    public Result findMemberById(Integer id) {
        Result result = physicalUploadService.findMemberById(id);
        return result;
    }


    @RequestMapping("/findPage")
    // 体检上传:初始化查询所有
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) throws Exception {
        PageResult pageResult = physicalUploadService.findPage(queryPageBean);
        return pageResult;
    }

    @RequestMapping("/deleteMember")
    // 体检上传:删除按钮,删除报告
    public Result deleteMember(Integer id) {
            Result result= physicalUploadService.deleteMemberInfo(id);
            return  result;
    }

    // 新增按钮,提交表单  上传表格,此时由于没有memberId,需要将上传的表格存到redis和表单提交时绑定
    @RequestMapping("/addMember")
    public Result addMember(@RequestBody Member member) {
        try {

            physicalUploadService.addMember(member);
            return new Result(true, "新增会员成功");
        } catch (Exception e) {
            return new Result(false, "新增会员失败");
        }
    }

    // 新增按钮:上传按钮  上传表格,此时由于没有memberId,需要将上传的表格存到redis和表单提交时绑定
    @RequestMapping("/upload")
    public Result upload(@RequestParam("excelFile") MultipartFile excelFile) throws IOException {
        try {
            //读取这个文件,得到每一行单元格的数组集合
            List<String[]> cellsList = POIUtils.readExcel(excelFile);
            if (cellsList != null && cellsList.size() > 0) {
                MemberInfo memberInfo = new MemberInfo();
                for (String[] cells : cellsList) {
                    //根据查看文件可知,cells[0]id身高 cells[1]是体重 cells[2]是舒张压 cells[3]收缩压 cells[4]
                    memberInfo.setHeight(cells[0]);
                    memberInfo.setWeight(cells[1]);
                    memberInfo.setDbp(cells[2]);
                    memberInfo.setSysp(cells[3]);
                }
                String json = JSON.toJSON(memberInfo).toString();
                jedisPool.getResource().setex("memberInfo", 300, json);
            } else {
                return new Result(false, "会员数据为空,无需导入");
            }
        } catch (IOException e) {
            return new Result(false, "上传会员数据失败");
        }
        return new Result(true, "上传会员数据成功");
    }


    // 编辑表单:上传表格
    @RequestMapping("/edit")
    public Result edit(MultipartFile excelFile) throws IOException {
        try {
            //读取这个文件,得到每一行单元格的数组集合
            List<String[]> cellsList = POIUtils.readExcel(excelFile);
            String memberid = jedisPool.getResource().get("memberid");
            if (memberid!=null){

            }
            if (cellsList != null && cellsList.size() > 0) {
            MemberInfo memberInfo = new MemberInfo();
            for (String[] cells : cellsList) {
                //根据查看文件可知,cells[0]id身高 cells[1]是体重 cells[2]是舒张压 cells[3]收缩压 cells[4]
                memberInfo.setId(Integer.parseInt(memberid));
                memberInfo.setHeight(cells[0]);
                memberInfo.setWeight(cells[1]);
                memberInfo.setDbp(cells[2]);
                memberInfo.setSysp(cells[3]);
            }
            //将保存了memberInfo对象通过service层放到数据库
            physicalUploadService.uploadTable(memberInfo);
        } else {
            return new Result(false, "会员数据为空,无需导入");
        }
    } catch (IOException e) {
        return new Result(false, "上传会员数据失败");
    }
        return new Result(true, "上传会员数据成功");
    }

}
